home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / syntax.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  50KB  |  1,726 lines

  1. /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
  2.    Copyright (C) 1985, 1987, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <ctype.h>
  23. #include "lisp.h"
  24. #include "commands.h"
  25. #include "buffer.h"
  26. #include "syntax.h"
  27.  
  28. #include "syntax_p.h"
  29. #include "insdel_p.h"
  30. #include "intervals_p.h"
  31. static int find_defun_start _P_((int pos));
  32. static void describe_syntax _P_((Lisp_Object value));
  33. static Lisp_Object describe_syntax_1 _P_((Lisp_Object vector));
  34. static int char_quoted _P_((register int pos));
  35. static void scan_sexps_forward _P_((struct lisp_parse_state *stateptr,
  36.                                     register int from, int end,
  37.                                     int targetdepth,
  38.                                     int stopbefore, Lisp_Object oldstate,
  39.                                     int commentstop));
  40.  
  41. Lisp_Object Qsyntax_table_p;
  42.  
  43. int words_include_escapes;
  44.  
  45. /* This is the internal form of the parse state used in parse-partial-sexp.  */
  46.  
  47. struct lisp_parse_state
  48.   {
  49.     int depth;        /* Depth at end of parsing */
  50.     int instring;    /* -1 if not within string, else desired terminator. */
  51.     int incomment;    /* Nonzero if within a comment at end of parsing */
  52.     int comstyle;    /* comment style a=0, or b=1 */
  53.     int quoted;        /* Nonzero if just after an escape char at end of parsing */
  54.     int thislevelstart;    /* Char number of most recent start-of-expression at current level */
  55.     int prevlevelstart; /* Char number of start of containing expression */
  56.     int location;    /* Char number at which parsing stopped. */
  57.     int mindepth;    /* Minimum depth seen while scanning.  */
  58.     int comstart;    /* Position just after last comment starter.  */
  59.   };
  60.  
  61. /* These variables are a cache for finding the start of a defun.
  62.    find_start_pos is the place for which the defun start was found.
  63.    find_start_value is the defun start position found for it.
  64.    find_start_buffer is the buffer it was found in.
  65.    find_start_begv is the BEGV value when it was found.
  66.    find_start_modiff is the value of MODIFF when it was found.  */
  67.  
  68. static int find_start_pos;
  69. static int find_start_value;
  70. static struct buffer *find_start_buffer;
  71. static int find_start_begv;
  72. static int find_start_modiff;
  73.  
  74. /* Find a defun-start that is the last one before POS (or nearly the last).
  75.    We record what we find, so that another call in the same area
  76.    can return the same value right away.  */
  77.  
  78. static int
  79. find_defun_start (pos)
  80.      int pos;
  81. {
  82.   int tem;
  83.   int shortage;
  84.  
  85.   /* Use previous finding, if it's valid and applies to this inquiry.  */
  86.   if (current_buffer == find_start_buffer
  87.       /* Reuse the defun-start even if POS is a little farther on.
  88.      POS might be in the next defun, but that's ok.
  89.      Our value may not be the best possible, but will still be usable.  */
  90.       && pos <= find_start_pos + 1000
  91.       && pos >= find_start_value
  92.       && BEGV == find_start_begv
  93.       && MODIFF == find_start_modiff)
  94.     return find_start_value;
  95.  
  96.   /* Back up to start of line.  */
  97.   tem = scan_buffer ('\n', pos, -1, &shortage);
  98.  
  99.   while (tem > BEGV)
  100.     {
  101.       /* Open-paren at start of line means we found our defun-start.  */
  102.       if (SYNTAX (FETCH_CHAR (tem)) == Sopen)
  103.     break;
  104.       /* Move to beg of previous line.  */
  105.       tem = scan_buffer ('\n', tem, -2, &shortage);
  106.     }
  107.  
  108.   /* Record what we found, for the next try.  */
  109.   find_start_value = tem;
  110.   find_start_buffer = current_buffer;
  111.   find_start_modiff = MODIFF;
  112.   find_start_begv = BEGV;
  113.   find_start_pos = pos;
  114.  
  115.   return find_start_value;
  116. }
  117.  
  118. DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
  119.   "Return t if ARG is a syntax table.\n\
  120. Any vector of 256 elements will do.")
  121.   (obj)
  122.      Lisp_Object obj;
  123. {
  124.   if (XTYPE (obj) == Lisp_Vector && XVECTOR (obj)->size == 0400)
  125.     return Qt;
  126.   return Qnil;
  127. }
  128.  
  129. Lisp_Object
  130. check_syntax_table (obj)
  131.      Lisp_Object obj;
  132. {
  133.   register Lisp_Object tem;
  134.   while (tem = Fsyntax_table_p (obj),
  135.      NILP (tem))
  136.     obj = wrong_type_argument (Qsyntax_table_p, obj);
  137.   return obj;
  138. }   
  139.  
  140.  
  141. DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
  142.   "Return the current syntax table.\n\
  143. This is the one specified by the current buffer.")
  144.   ()
  145. {
  146.   return current_buffer->syntax_table;
  147. }
  148.  
  149. DEFUN ("standard-syntax-table", Fstandard_syntax_table,
  150.    Sstandard_syntax_table, 0, 0, 0,
  151.   "Return the standard syntax table.\n\
  152. This is the one used for new buffers.")
  153.   ()
  154. {
  155.   return Vstandard_syntax_table;
  156. }
  157.  
  158. DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
  159.   "Construct a new syntax table and return it.\n\
  160. It is a copy of the TABLE, which defaults to the standard syntax table.")
  161.   (table)
  162.      Lisp_Object table;
  163. {
  164.   Lisp_Object size, val;
  165.   XFASTINT (size) = 0400;
  166.   XFASTINT (val) = 0;
  167.   val = Fmake_vector (size, val);
  168.   if (!NILP (table))
  169.     table = check_syntax_table (table);
  170.   else if (NILP (Vstandard_syntax_table))
  171.     /* Can only be null during initialization */
  172.     return val;
  173.   else table = Vstandard_syntax_table;
  174.  
  175.   bcopy (XVECTOR (table)->contents,
  176.      XVECTOR (val)->contents, 0400 * sizeof (Lisp_Object));
  177.   return val;
  178. }
  179.  
  180. DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
  181.   "Select a new syntax table for the current buffer.\n\
  182. One argument, a syntax table.")
  183.   (table)
  184.      Lisp_Object table;
  185. {
  186.   table = check_syntax_table (table);
  187.   current_buffer->syntax_table = table;
  188.   /* Indicate that this buffer now has a specified syntax table.  */
  189.   current_buffer->local_var_flags
  190.     |= XFASTINT (buffer_local_flags.syntax_table);
  191.   return table;
  192. }
  193.  
  194. /* Convert a letter which signifies a syntax code
  195.  into the code it signifies.
  196.  This is used by modify-syntax-entry, and other things. */
  197.  
  198. unsigned char syntax_spec_code[0400] =
  199.   { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  200.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  201.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  202.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  203.     (char) Swhitespace, 0377, (char) Sstring, 0377,
  204.         (char) Smath, 0377, 0377, (char) Squote,
  205.     (char) Sopen, (char) Sclose, 0377, 0377,
  206.     0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
  207.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  208.     0377, 0377, 0377, 0377,
  209.     (char) Scomment, 0377, (char) Sendcomment, 0377,
  210.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,   /* @, A, ... */
  211.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  212.     0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
  213.     0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
  214.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,   /* `, a, ... */
  215.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  216.     0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
  217.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377
  218.   };
  219.  
  220. /* Indexed by syntax code, give the letter that describes it. */
  221.  
  222. char syntax_code_spec[13] =
  223.   {
  224.     ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>'
  225.   };
  226.  
  227. DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
  228.   "Return the syntax code of CHAR, described by a character.\n\
  229. For example, if CHAR is a word constituent, the character `?w' is returned.\n\
  230. The characters that correspond to various syntax codes\n\
  231. are listed in the documentation of `modify-syntax-entry'.")
  232.   (ch)
  233.      Lisp_Object ch;
  234. {
  235.   CHECK_NUMBER (ch, 0);
  236.   return make_number (syntax_code_spec[(int) SYNTAX (0xFF & XINT (ch))]);
  237. }
  238.  
  239. /* This comment supplies the doc string for modify-syntax-entry,
  240.    for make-docfile to see.  We cannot put this in the real DEFUN
  241.    due to limits in the Unix cpp.
  242.  
  243. DEFUN ("modify-syntax-entry", foo, bar, 2, 3, 0,
  244.   "Set syntax for character CHAR according to string S.\n\
  245. The syntax is changed only for table TABLE, which defaults to\n\
  246.  the current buffer's syntax table.\n\
  247. The first character of S should be one of the following:\n\
  248.   Space or -  whitespace syntax.    w   word constituent.\n\
  249.   _           symbol constituent.   .   punctuation.\n\
  250.   (           open-parenthesis.     )   close-parenthesis.\n\
  251.   \"           string quote.         \\   escape.\n\
  252.   $           paired delimiter.     '   expression quote or prefix operator.\n\
  253.   <           comment starter.      >   comment ender.\n\                  
  254.   /           character-quote.\n\
  255. Only single-character comment start and end sequences are represented thus.\n\
  256. Two-character sequences are represented as described below.\n\
  257. The second character of S is the matching parenthesis,\n\
  258.  used only if the first character is `(' or `)'.\n\
  259. Any additional characters are flags.\n\
  260. Defined flags are the characters 1, 2, 3, 4, b, and p.\n\
  261.  1 means C is the start of a two-char comment start sequence.\n\
  262.  2 means C is the second character of such a sequence.\n\
  263.  3 means C is the start of a two-char comment end sequence.\n\
  264.  4 means C is the second character of such a sequence.\n\
  265. \n\
  266. There can be up to two orthogonal comment sequences. This is to support\n\
  267. language modes such as C++.  By default, all comment sequences are of style\n\
  268. a, but you can set the comment sequence style to b (on the second character of a\n\
  269. comment-start, or the first character of a comment-end sequence) by using\n\
  270. this flag:\n\
  271.  b means C is part of comment sequence b.\n\
  272. \n\
  273.  p means C is a prefix character for `backward-prefix-chars';\n\
  274.    such characters are treated as whitespace when they occur\n\
  275.    between expressions.")
  276.   (char, s, table)
  277. */
  278.  
  279. DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3, 
  280.   /* I really don't know why this is interactive
  281.      help-form should at least be made useful whilst reading the second arg
  282.    */
  283.   "cSet syntax for character: \nsSet syntax for %s to: ",
  284.   0 /* See immediately above */)
  285.   (c, newentry, syntax_table)
  286.      Lisp_Object c, newentry, syntax_table;
  287. {
  288.   register unsigned char *p, match;
  289.   register enum syntaxcode code;
  290.   Lisp_Object val;
  291.  
  292.   CHECK_NUMBER (c, 0);
  293.   CHECK_STRING (newentry, 1);
  294.   if (NILP (syntax_table))
  295.     syntax_table = current_buffer->syntax_table;
  296.   else
  297.     syntax_table = check_syntax_table (syntax_table);
  298.  
  299.   p = XSTRING (newentry)->data;
  300.   code = (enum syntaxcode) syntax_spec_code[*p++];
  301.   if (((int) code & 0377) == 0377)
  302.     error ("invalid syntax description letter: %c", c);
  303.  
  304.   match = *p;
  305.   if (match) p++;
  306.   if (match == ' ') match = 0;
  307.  
  308.   XFASTINT (val) = (match << 8) + (int) code;
  309.   while (*p)
  310.     switch (*p++)
  311.       {
  312.       case '1':
  313.     XFASTINT (val) |= 1 << 16;
  314.     break;
  315.  
  316.       case '2':
  317.     XFASTINT (val) |= 1 << 17;
  318.     break;
  319.  
  320.       case '3':
  321.     XFASTINT (val) |= 1 << 18;
  322.     break;
  323.  
  324.       case '4':
  325.     XFASTINT (val) |= 1 << 19;
  326.     break;
  327.  
  328.       case 'p':
  329.     XFASTINT (val) |= 1 << 20;
  330.     break;
  331.  
  332.       case 'b':
  333.     XFASTINT (val) |= 1 << 21;
  334.     break;
  335.       }
  336.     
  337.   XVECTOR (syntax_table)->contents[0xFF & XINT (c)] = val;
  338.  
  339.   return Qnil;
  340. }
  341.  
  342. /* Dump syntax table to buffer in human-readable format */
  343.  
  344. static void
  345. describe_syntax (value)
  346.     Lisp_Object value;
  347. {
  348.   register enum syntaxcode code;
  349.   char desc, match, start1, start2, end1, end2, prefix, comstyle;
  350.   char str[2];
  351.  
  352.   Findent_to (make_number (16), make_number (1));
  353.  
  354.   if (XTYPE (value) != Lisp_Int)
  355.     {
  356.       insert_string ("invalid");
  357.       return;
  358.     }
  359.  
  360.   code = (enum syntaxcode) (XINT (value) & 0377);
  361.   match = (XINT (value) >> 8) & 0377;
  362.   start1 = (XINT (value) >> 16) & 1;
  363.   start2 = (XINT (value) >> 17) & 1;
  364.   end1 = (XINT (value) >> 18) & 1;
  365.   end2 = (XINT (value) >> 19) & 1;
  366.   prefix = (XINT (value) >> 20) & 1;
  367.   comstyle = (XINT (value) >> 21) & 1;
  368.  
  369.   if ((int) code < 0 || (int) code >= (int) Smax)
  370.     {
  371.       insert_string ("invalid");
  372.       return;
  373.     }
  374.   desc = syntax_code_spec[(int) code];
  375.  
  376.   str[0] = desc, str[1] = 0;
  377.   insert (str, 1);
  378.  
  379.   str[0] = match ? match : ' ';
  380.   insert (str, 1);
  381.  
  382.  
  383.   if (start1)
  384.     insert ("1", 1);
  385.   if (start2)
  386.     insert ("2", 1);
  387.  
  388.   if (end1)
  389.     insert ("3", 1);
  390.   if (end2)
  391.     insert ("4", 1);
  392.  
  393.   if (prefix)
  394.     insert ("p", 1);
  395.   if (comstyle)
  396.     insert ("b", 1);
  397.  
  398.   insert_string ("\twhich means: ");
  399.  
  400. #ifdef SWITCH_ENUM_BUG
  401.   switch ((int) code)
  402. #else
  403.   switch (code)
  404. #endif
  405.     {
  406.     case Swhitespace:
  407.       insert_string ("whitespace"); break;
  408.     case Spunct:
  409.       insert_string ("punctuation"); break;
  410.     case Sword:
  411.       insert_string ("word"); break;
  412.     case Ssymbol:
  413.       insert_string ("symbol"); break;
  414.     case Sopen:
  415.       insert_string ("open"); break;
  416.     case Sclose:
  417.       insert_string ("close"); break;
  418.     case Squote:
  419.       insert_string ("quote"); break;
  420.     case Sstring:
  421.       insert_string ("string"); break;
  422.     case Smath:
  423.       insert_string ("math"); break;
  424.     case Sescape:
  425.       insert_string ("escape"); break;
  426.     case Scharquote:
  427.       insert_string ("charquote"); break;
  428.     case Scomment:
  429.       insert_string ("comment"); break;
  430.     case Sendcomment:
  431.       insert_string ("endcomment"); break;
  432.     default:
  433.       insert_string ("invalid");
  434.       return;
  435.     }
  436.  
  437.   if (match)
  438.     {
  439.       insert_string (", matches ");
  440.       insert_char (match);
  441.     }
  442.  
  443.   if (start1)
  444.     insert_string (",\n\t  is the first character of a comment-start sequence");
  445.   if (start2)
  446.     insert_string (",\n\t  is the second character of a comment-start sequence");
  447.  
  448.   if (end1)
  449.     insert_string (",\n\t  is the first character of a comment-end sequence");
  450.   if (end2)
  451.     insert_string (",\n\t  is the second character of a comment-end sequence");
  452.   if (comstyle)
  453.     insert_string (" (comment style b)");
  454.  
  455.   if (prefix)
  456.     insert_string (",\n\t  is a prefix character for `backward-prefix-chars'");
  457.  
  458.   insert_string ("\n");
  459. }
  460.  
  461. static Lisp_Object
  462. describe_syntax_1 (vector)
  463.      Lisp_Object vector;
  464. {
  465.   struct buffer *old = current_buffer;
  466.   set_buffer_internal (XBUFFER (Vstandard_output));
  467.   describe_vector (vector, Qnil, describe_syntax, 0, Qnil);
  468.   set_buffer_internal (old);
  469.   return Qnil;
  470. }
  471.  
  472. DEFUN ("describe-syntax", Fdescribe_syntax, Sdescribe_syntax, 0, 0, "",
  473.   "Describe the syntax specifications in the syntax table.\n\
  474. The descriptions are inserted in a buffer, which is then displayed.")
  475.   ()
  476. {
  477.   internal_with_output_to_temp_buffer
  478.      ("*Help*", describe_syntax_1, current_buffer->syntax_table);
  479.  
  480.   return Qnil;
  481. }
  482.  
  483. /* Return the position across COUNT words from FROM.
  484.    If that many words cannot be found before the end of the buffer, return 0.
  485.    COUNT negative means scan backward and stop at word beginning.  */
  486.  
  487. int
  488. scan_words (from, count)
  489.      register int from, count;
  490. {
  491.   register int beg = BEGV;
  492.   register int end = ZV;
  493.   register int code;
  494.  
  495.   immediate_quit = 1;
  496.   QUIT;
  497.  
  498.   while (count > 0)
  499.     {
  500.       while (1)
  501.     {
  502.       if (from == end)
  503.         {
  504.           immediate_quit = 0;
  505.           return 0;
  506.         }
  507.       code = SYNTAX (FETCH_CHAR (from));
  508.       if (words_include_escapes
  509.           && (code == Sescape || code == Scharquote))
  510.         break;
  511.       if (code == Sword)
  512.         break;
  513.       from++;
  514.     }
  515.       while (1)
  516.     {
  517.       if (from == end) break;
  518.       code = SYNTAX (FETCH_CHAR (from));
  519.       if (!(words_include_escapes
  520.         && (code == Sescape || code == Scharquote)))
  521.         if (code != Sword)
  522.           break;
  523.       from++;
  524.     }
  525.       count--;
  526.     }
  527.   while (count < 0)
  528.     {
  529.       while (1)
  530.     {
  531.       if (from == beg)
  532.         {
  533.           immediate_quit = 0;
  534.           return 0;
  535.         }
  536.       code = SYNTAX (FETCH_CHAR (from - 1));
  537.       if (words_include_escapes
  538.           && (code == Sescape || code == Scharquote))
  539.         break;
  540.       if (code == Sword)
  541.         break;
  542.       from--;
  543.     }
  544.       while (1)
  545.     {
  546.       if (from == beg) break;
  547.       code = SYNTAX (FETCH_CHAR (from - 1));
  548.       if (!(words_include_escapes
  549.         && (code == Sescape || code == Scharquote)))
  550.         if (code != Sword)
  551.           break;
  552.       from--;
  553.     }
  554.       count++;
  555.     }
  556.  
  557.   immediate_quit = 0;
  558.  
  559.   return from;
  560. }
  561.  
  562. DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 1, "p",
  563.   "Move point forward ARG words (backward if ARG is negative).\n\
  564. Normally returns t.\n\
  565. If an edge of the buffer is reached, point is left there\n\
  566. and nil is returned.")
  567.   (count)
  568.      Lisp_Object count;
  569. {
  570.   int val;
  571.   CHECK_NUMBER (count, 0);
  572.  
  573.   if (!(val = scan_words (point, XINT (count))))
  574.     {
  575.       SET_PT (XINT (count) > 0 ? ZV : BEGV);
  576.       return Qnil;
  577.     }
  578.   SET_PT (val);
  579.   return Qt;
  580. }
  581.  
  582. DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
  583.   "Move forward across up to N comments.  If N is negative, move backward.\n\
  584. Stop scanning if we find something other than a comment or whitespace.\n\
  585. Set point to where scanning stops.\n\
  586. If N comments are found as expected, with nothing except whitespace\n\
  587. between them, return t; otherwise return nil.")
  588.   (count)
  589.      Lisp_Object count;
  590. {
  591.   register int from;
  592.   register int stop;
  593.   register int c;
  594.   register enum syntaxcode code;
  595.   int comstyle = 0;        /* style of comment encountered */
  596.   int count1;
  597.  
  598.   CHECK_NUMBER (count, 0);
  599.   count1 = XINT (count);
  600.  
  601.   immediate_quit = 1;
  602.   QUIT;
  603.  
  604.   from = PT;
  605.  
  606.   while (count1 > 0)
  607.     {
  608.       stop = ZV;
  609.       while (from < stop)
  610.     {
  611.       c = FETCH_CHAR (from);
  612.       code = SYNTAX (c);
  613.       from++;
  614.       comstyle = 0;
  615.       if (from < stop && SYNTAX_COMSTART_FIRST (c)
  616.           && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from)))
  617.         {
  618.           /* we have encountered a comment start sequence and we 
  619.          are ignoring all text inside comments. we must record
  620.          the comment style this sequence begins so that later,
  621.          only a comment end of the same style actually ends
  622.          the comment section */
  623.           code = Scomment;
  624.           comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from));
  625.           from++;
  626.         }
  627.  
  628.       if (code == Scomment)
  629.         {
  630.           while (1)
  631.         {
  632.           if (from == stop)
  633.             {
  634.               immediate_quit = 0;
  635.               SET_PT (from);
  636.               return Qnil;
  637.             }
  638.           c = FETCH_CHAR (from);
  639.           if (SYNTAX (c) == Sendcomment
  640.               && SYNTAX_COMMENT_STYLE (c) == comstyle)
  641.             /* we have encountered a comment end of the same style
  642.                as the comment sequence which began this comment
  643.                section */
  644.             break;
  645.           from++;
  646.           if (from < stop && SYNTAX_COMEND_FIRST (c)
  647.               && SYNTAX_COMEND_SECOND (FETCH_CHAR (from))
  648.               && SYNTAX_COMMENT_STYLE (c) == comstyle)
  649.             /* we have encountered a comment end of the same style
  650.                as the comment sequence which began this comment
  651.                section */
  652.             { from++; break; }
  653.         }
  654.           /* We have skipped one comment.  */
  655.           break;
  656.         }
  657.       else if (code != Swhitespace && code != Sendcomment)
  658.         {
  659.           immediate_quit = 0;
  660.           SET_PT (from - 1);
  661.           return Qnil;
  662.         }
  663.     }
  664.  
  665.       /* End of comment reached */
  666.       count1--;
  667.     }
  668.  
  669.   while (count1 < 0)
  670.     {
  671.       stop = BEGV;
  672.       while (from > stop)
  673.     {
  674.       int quoted;
  675.  
  676.       from--;
  677.       quoted = char_quoted (from);
  678.       if (quoted)
  679.         from--;
  680.       c = FETCH_CHAR (from);
  681.       code = SYNTAX (c);
  682.       comstyle = 0;
  683.       if (from > stop && SYNTAX_COMEND_SECOND (c)
  684.           && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1))
  685.           && !char_quoted (from - 1))
  686.         {
  687.           /* we must record the comment style encountered so that
  688.          later, we can match only the proper comment begin
  689.          sequence of the same style */
  690.           code = Sendcomment;
  691.           comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from - 1));
  692.           from--;
  693.         }
  694.  
  695.       if (code == Sendcomment && !quoted)
  696.         {
  697. #if 0
  698.           if (code != SYNTAX (c))
  699.         /* For a two-char comment ender, we can assume
  700.            it does end a comment.  So scan back in a simple way.  */
  701.         {
  702.           if (from != stop) from--;
  703.           while (1)
  704.             {
  705.               if (SYNTAX (c = FETCH_CHAR (from)) == Scomment
  706.               && SYNTAX_COMMENT_STYLE (c) == comstyle)
  707.             break;
  708.               if (from == stop)
  709.             {
  710.               immediate_quit = 0;
  711.               SET_PT (from);
  712.               return Qnil;
  713.             }
  714.               from--;
  715.               if (SYNTAX_COMSTART_SECOND (c)
  716.               && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from))
  717.               && SYNTAX_COMMENT_STYLE (c) == comstyle
  718.               && !char_quoted (from))
  719.             break;
  720.             }
  721.           break;
  722.         }
  723. #endif /* 0 */
  724.  
  725.           /* Look back, counting the parity of string-quotes,
  726.          and recording the comment-starters seen.
  727.          When we reach a safe place, assume that's not in a string;
  728.          then step the main scan to the earliest comment-starter seen
  729.          an even number of string quotes away from the safe place.
  730.  
  731.          OFROM[I] is position of the earliest comment-starter seen
  732.          which is I+2X quotes from the comment-end.
  733.          PARITY is current parity of quotes from the comment end.  */
  734.           {
  735.         int parity = 0;
  736.         char my_stringend = 0;
  737.         int string_lossage = 0;
  738.         int comment_end = from;
  739.         int comstart_pos = 0;
  740.         int comstart_parity = 0;
  741.  
  742.         /* At beginning of range to scan, we're outside of strings;
  743.            that determines quote parity to the comment-end.  */
  744.         while (from != stop)
  745.           {
  746.             /* Move back and examine a character.  */
  747.             from--;
  748.  
  749.             c = FETCH_CHAR (from);
  750.             code = SYNTAX (c);
  751.  
  752.             /* If this char is the second of a 2-char comment sequence,
  753.                back up and give the pair the appropriate syntax.  */
  754.             if (from > stop && SYNTAX_COMEND_SECOND (c)
  755.             && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1)))
  756.               {
  757.             code = Sendcomment;
  758.             from--;
  759.               }
  760.             
  761.             else if (from > stop && SYNTAX_COMSTART_SECOND (c)
  762.                  && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from - 1))
  763.                  && comstyle == SYNTAX_COMMENT_STYLE (c))
  764.               {
  765.             code = Scomment;
  766.             from--;
  767.               }
  768.  
  769.             /* Ignore escaped characters.  */
  770.             if (char_quoted (from))
  771.               continue;
  772.  
  773.             /* Track parity of quotes.  */
  774.             if (code == Sstring)
  775.               {
  776.             parity ^= 1;
  777.             if (my_stringend == 0)
  778.               my_stringend = c;
  779.             /* If we have two kinds of string delimiters.
  780.                There's no way to grok this scanning backwards.  */
  781.             else if (my_stringend != c)
  782.               string_lossage = 1;
  783.               }
  784.  
  785.             /* Record comment-starters according to that
  786.                quote-parity to the comment-end.  */
  787.             if (code == Scomment)
  788.               {
  789.             comstart_parity = parity;
  790.             comstart_pos = from;
  791.               }
  792.  
  793.             /* If we find another earlier comment-ender,
  794.                any comment-starts earlier than that don't count
  795.                (because they go with the earlier comment-ender).  */
  796.             if (code == Sendcomment
  797.             && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)) == comstyle)
  798.               break;
  799.  
  800.             /* Assume a defun-start point is outside of strings.  */
  801.             if (code == Sopen
  802.             && (from == stop || FETCH_CHAR (from - 1) == '\n'))
  803.               break;
  804.           }
  805.  
  806.         if (comstart_pos == 0)
  807.           from = comment_end;
  808.         /* If the earliest comment starter
  809.            is followed by uniform paired string quotes or none,
  810.            we know it can't be inside a string
  811.            since if it were then the comment ender would be inside one.
  812.            So it does start a comment.  Skip back to it.  */
  813.         else if (comstart_parity == 0 && !string_lossage)
  814.           from = comstart_pos;
  815.         else
  816.           {
  817.             /* We had two kinds of string delimiters mixed up
  818.                together.  Decode this going forwards.
  819.                Scan fwd from the previous comment ender
  820.                to the one in question; this records where we
  821.                last passed a comment starter.  */
  822.             struct lisp_parse_state state;
  823.             scan_sexps_forward (&state, find_defun_start (comment_end),
  824.                     comment_end - 1, -10000, 0, Qnil, 0);
  825.             if (state.incomment)
  826.               from = state.comstart;
  827.             else
  828.               /* We can't grok this as a comment; scan it normally.  */
  829.               from = comment_end;
  830.           }
  831.           }
  832.         }
  833.       else if ((code != Swhitespace && code != Scomment) || quoted)
  834.         {
  835.           immediate_quit = 0;
  836.           SET_PT (from + 1);
  837.           return Qnil;
  838.         }
  839.     }
  840.  
  841.       count1++;
  842.     }
  843.  
  844.   SET_PT (from);
  845.   immediate_quit = 0;
  846.   return Qt;
  847. }
  848.  
  849. int parse_sexp_ignore_comments;
  850.  
  851. Lisp_Object
  852. scan_lists (from, count, depth, sexpflag)
  853.      register int from;
  854.      int count, depth, sexpflag;
  855. {
  856.   Lisp_Object val;
  857.   register int stop;
  858.   register int c;
  859.   char stringterm;
  860.   int quoted;
  861.   int mathexit = 0;
  862.   register enum syntaxcode code;
  863.   int min_depth = depth;    /* Err out if depth gets less than this. */
  864.   int comstyle = 0;        /* style of comment encountered */
  865.  
  866.   if (depth > 0) min_depth = 0;
  867.  
  868.   immediate_quit = 1;
  869.   QUIT;
  870.  
  871.   while (count > 0)
  872.     {
  873.       stop = ZV;
  874.       while (from < stop)
  875.     {
  876.       c = FETCH_CHAR (from);
  877.       code = SYNTAX (c);
  878.       from++;
  879.       if (from < stop && SYNTAX_COMSTART_FIRST (c)
  880.           && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from))
  881.           && parse_sexp_ignore_comments)
  882.         {
  883.           /* we have encountered a comment start sequence and we 
  884.          are ignoring all text inside comments. we must record
  885.          the comment style this sequence begins so that later,
  886.          only a comment end of the same style actually ends
  887.          the comment section */
  888.           code = Scomment;
  889.           comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from));
  890.           from++;
  891.         }
  892.       
  893.       if (SYNTAX_PREFIX (c))
  894.         continue;
  895.  
  896. #ifdef SWITCH_ENUM_BUG
  897.       switch ((int) code)
  898. #else
  899.       switch (code)
  900. #endif
  901.         {
  902.         case Sescape:
  903.         case Scharquote:
  904.           if (from == stop) goto lose;
  905.           from++;
  906.           /* treat following character as a word constituent */
  907.         case Sword:
  908.         case Ssymbol:
  909.           if (depth || !sexpflag) break;
  910.           /* This word counts as a sexp; return at end of it. */
  911.           while (from < stop)
  912.         {
  913. #ifdef SWITCH_ENUM_BUG
  914.           switch ((int) SYNTAX (FETCH_CHAR (from)))
  915. #else
  916.           switch (SYNTAX (FETCH_CHAR (from)))
  917. #endif
  918.             {
  919.             case Scharquote:
  920.             case Sescape:
  921.               from++;
  922.               if (from == stop) goto lose;
  923.               break;
  924.             case Sword:
  925.             case Ssymbol:
  926.             case Squote:
  927.               break;
  928.             default:
  929.               goto done;
  930.             }
  931.           from++;
  932.         }
  933.           goto done;
  934.  
  935.         case Scomment:
  936.           if (!parse_sexp_ignore_comments) break;
  937.           while (1)
  938.         {
  939.           if (from == stop) goto done;
  940.           c = FETCH_CHAR (from);
  941.           if (SYNTAX (c) == Sendcomment
  942.               && SYNTAX_COMMENT_STYLE (c) == comstyle)
  943.             /* we have encountered a comment end of the same style
  944.                as the comment sequence which began this comment
  945.                section */
  946.             break;
  947.           from++;
  948.           if (from < stop && SYNTAX_COMEND_FIRST (c)
  949.               && SYNTAX_COMEND_SECOND (FETCH_CHAR (from))
  950.               && SYNTAX_COMMENT_STYLE (c) == comstyle)
  951.             /* we have encountered a comment end of the same style
  952.                as the comment sequence which began this comment
  953.                section */
  954.             { from++; break; }
  955.         }
  956.           break;
  957.  
  958.         case Smath:
  959.           if (!sexpflag)
  960.         break;
  961.           if (from != stop && c == FETCH_CHAR (from))
  962.         from++;
  963.           if (mathexit)
  964.         {
  965.           mathexit = 0;
  966.           goto close1;
  967.         }
  968.           mathexit = 1;
  969.  
  970.         case Sopen:
  971.           if (!++depth) goto done;
  972.           break;
  973.  
  974.         case Sclose:
  975.         close1:
  976.           if (!--depth) goto done;
  977.           if (depth < min_depth)
  978.         error ("Containing expression ends prematurely");
  979.           break;
  980.  
  981.         case Sstring:
  982.           stringterm = FETCH_CHAR (from - 1);
  983.           while (1)
  984.         {
  985.           if (from >= stop) goto lose;
  986.           if (FETCH_CHAR (from) == stringterm) break;
  987. #ifdef SWITCH_ENUM_BUG
  988.           switch ((int) SYNTAX (FETCH_CHAR (from)))
  989. #else
  990.           switch (SYNTAX (FETCH_CHAR (from)))
  991. #endif
  992.             {
  993.             case Scharquote:
  994.             case Sescape:
  995.               from++;
  996.             }
  997.           from++;
  998.         }
  999.           from++;
  1000.           if (!depth && sexpflag) goto done;
  1001.           break;
  1002.         }
  1003.     }
  1004.  
  1005.       /* Reached end of buffer.  Error if within object, return nil if between */
  1006.       if (depth) goto lose;
  1007.  
  1008.       immediate_quit = 0;
  1009.       return Qnil;
  1010.  
  1011.       /* End of object reached */
  1012.     done:
  1013.       count--;
  1014.     }
  1015.  
  1016.   while (count < 0)
  1017.     {
  1018.       stop = BEGV;
  1019.       while (from > stop)
  1020.     {
  1021.       from--;
  1022.       if (quoted = char_quoted (from))
  1023.         from--;
  1024.       c = FETCH_CHAR (from);
  1025.       code = SYNTAX (c);
  1026.       if (from > stop && SYNTAX_COMEND_SECOND (c)
  1027.           && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1))
  1028.           && !char_quoted (from - 1)
  1029.           && parse_sexp_ignore_comments)
  1030.         {
  1031.           /* we must record the comment style encountered so that
  1032.          later, we can match only the proper comment begin
  1033.          sequence of the same style */
  1034.           code = Sendcomment;
  1035.           comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from - 1));
  1036.           from--;
  1037.         }
  1038.       
  1039.       if (SYNTAX_PREFIX (c))
  1040.         continue;
  1041.  
  1042. #ifdef SWITCH_ENUM_BUG
  1043.       switch ((int) (quoted ? Sword : code))
  1044. #else
  1045.       switch (quoted ? Sword : code)
  1046. #endif
  1047.         {
  1048.         case Sword:
  1049.         case Ssymbol:
  1050.           if (depth || !sexpflag) break;
  1051.           /* This word counts as a sexp; count object finished after passing it. */
  1052.           while (from > stop)
  1053.         {
  1054.           quoted = char_quoted (from - 1);
  1055.           if (quoted)
  1056.             from--;
  1057.           if (! (quoted || SYNTAX (FETCH_CHAR (from - 1)) == Sword
  1058.              || SYNTAX (FETCH_CHAR (from - 1)) == Ssymbol
  1059.              || SYNTAX (FETCH_CHAR (from - 1)) == Squote))
  1060.                     goto done2;
  1061.           from--;
  1062.         }
  1063.           goto done2;
  1064.  
  1065.         case Smath:
  1066.           if (!sexpflag)
  1067.         break;
  1068.           if (from != stop && c == FETCH_CHAR (from - 1))
  1069.         from--;
  1070.           if (mathexit)
  1071.         {
  1072.           mathexit = 0;
  1073.           goto open2;
  1074.         }
  1075.           mathexit = 1;
  1076.  
  1077.         case Sclose:
  1078.           if (!++depth) goto done2;
  1079.           break;
  1080.  
  1081.         case Sopen:
  1082.         open2:
  1083.           if (!--depth) goto done2;
  1084.           if (depth < min_depth)
  1085.         error ("Containing expression ends prematurely");
  1086.           break;
  1087.  
  1088.         case Sendcomment:
  1089.           if (!parse_sexp_ignore_comments)
  1090.         break;
  1091. #if 0
  1092.           if (code != SYNTAX (c))
  1093.         /* For a two-char comment ender, we can assume
  1094.            it does end a comment.  So scan back in a simple way.  */
  1095.         {
  1096.           if (from != stop) from--;
  1097.           while (1)
  1098.             {
  1099.               if (SYNTAX (c = FETCH_CHAR (from)) == Scomment
  1100.               && SYNTAX_COMMENT_STYLE (c) == comstyle)
  1101.             break;
  1102.               if (from == stop) goto done;
  1103.               from--;
  1104.               if (SYNTAX_COMSTART_SECOND (c)
  1105.               && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from))
  1106.               && SYNTAX_COMMENT_STYLE (c) == comstyle
  1107.               && !char_quoted (from))
  1108.             break;
  1109.             }
  1110.           break;
  1111.         }
  1112. #endif /* 0 */
  1113.  
  1114.           /* Look back, counting the parity of string-quotes,
  1115.          and recording the comment-starters seen.
  1116.          When we reach a safe place, assume that's not in a string;
  1117.          then step the main scan to the earliest comment-starter seen
  1118.          an even number of string quotes away from the safe place.
  1119.  
  1120.          OFROM[I] is position of the earliest comment-starter seen
  1121.          which is I+2X quotes from the comment-end.
  1122.          PARITY is current parity of quotes from the comment end.  */
  1123.           {
  1124.         int parity = 0;
  1125.         char my_stringend = 0;
  1126.         int string_lossage = 0;
  1127.         int comment_end = from;
  1128.         int comstart_pos = 0;
  1129.         int comstart_parity = 0;
  1130.  
  1131.         /* At beginning of range to scan, we're outside of strings;
  1132.            that determines quote parity to the comment-end.  */
  1133.         while (from != stop)
  1134.           {
  1135.             /* Move back and examine a character.  */
  1136.             from--;
  1137.  
  1138.             c = FETCH_CHAR (from);
  1139.             code = SYNTAX (c);
  1140.  
  1141.             /* If this char is the second of a 2-char comment sequence,
  1142.                back up and give the pair the appropriate syntax.  */
  1143.             if (from > stop && SYNTAX_COMEND_SECOND (c)
  1144.             && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1)))
  1145.               {
  1146.             code = Sendcomment;
  1147.             from--;
  1148.               }
  1149.             
  1150.             else if (from > stop && SYNTAX_COMSTART_SECOND (c)
  1151.                  && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from - 1))
  1152.                  && comstyle == SYNTAX_COMMENT_STYLE (c))
  1153.               {
  1154.             code = Scomment;
  1155.             from--;
  1156.               }
  1157.  
  1158.             /* Ignore escaped characters.  */
  1159.             if (char_quoted (from))
  1160.               continue;
  1161.  
  1162.             /* Track parity of quotes.  */
  1163.             if (code == Sstring)
  1164.               {
  1165.             parity ^= 1;
  1166.             if (my_stringend == 0)
  1167.               my_stringend = c;
  1168.             /* If we have two kinds of string delimiters.
  1169.                There's no way to grok this scanning backwards.  */
  1170.             else if (my_stringend != c)
  1171.               string_lossage = 1;
  1172.               }
  1173.  
  1174.             /* Record comment-starters according to that
  1175.                quote-parity to the comment-end.  */
  1176.             if (code == Scomment)
  1177.               {
  1178.             comstart_parity = parity;
  1179.             comstart_pos = from;
  1180.               }
  1181.  
  1182.             /* If we find another earlier comment-ender,
  1183.                any comment-starts earlier than that don't count
  1184.                (because they go with the earlier comment-ender).  */
  1185.             if (code == Sendcomment
  1186.             && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)) == comstyle)
  1187.               break;
  1188.  
  1189.             /* Assume a defun-start point is outside of strings.  */
  1190.             if (code == Sopen
  1191.             && (from == stop || FETCH_CHAR (from - 1) == '\n'))
  1192.               break;
  1193.           }
  1194.  
  1195.         if (comstart_pos == 0)
  1196.           from = comment_end;
  1197.         /* If the earliest comment starter
  1198.            is followed by uniform paired string quotes or none,
  1199.            we know it can't be inside a string
  1200.            since if it were then the comment ender would be inside one.
  1201.            So it does start a comment.  Skip back to it.  */
  1202.         else if (comstart_parity == 0 && !string_lossage)
  1203.           from = comstart_pos;
  1204.         else
  1205.           {
  1206.             /* We had two kinds of string delimiters mixed up
  1207.                together.  Decode this going forwards.
  1208.                Scan fwd from the previous comment ender
  1209.                to the one in question; this records where we
  1210.                last passed a comment starter.  */
  1211.             struct lisp_parse_state state;
  1212.             scan_sexps_forward (&state, find_defun_start (comment_end),
  1213.                     comment_end - 1, -10000, 0, Qnil, 0);
  1214.             if (state.incomment)
  1215.               from = state.comstart;
  1216.             else
  1217.               /* We can't grok this as a comment; scan it normally.  */
  1218.               from = comment_end;
  1219.           }
  1220.           }
  1221.           break;
  1222.  
  1223.         case Sstring:
  1224.           stringterm = FETCH_CHAR (from);
  1225.           while (1)
  1226.         {
  1227.           if (from == stop) goto lose;
  1228.           if (!char_quoted (from - 1)
  1229.               && stringterm == FETCH_CHAR (from - 1))
  1230.             break;
  1231.           from--;
  1232.         }
  1233.           from--;
  1234.           if (!depth && sexpflag) goto done2;
  1235.           break;
  1236.         }
  1237.     }
  1238.  
  1239.       /* Reached start of buffer.  Error if within object, return nil if between */
  1240.       if (depth) goto lose;
  1241.  
  1242.       immediate_quit = 0;
  1243.       return Qnil;
  1244.  
  1245.     done2:
  1246.       count++;
  1247.     }
  1248.  
  1249.  
  1250.   immediate_quit = 0;
  1251.   XFASTINT (val) = from;
  1252.   return val;
  1253.  
  1254.  lose:
  1255.   error ("Unbalanced parentheses");
  1256.   /* NOTREACHED */
  1257. }
  1258.  
  1259. static int
  1260. char_quoted (pos)
  1261.      register int pos;
  1262. {
  1263.   register enum syntaxcode code;
  1264.   register int beg = BEGV;
  1265.   register int quoted = 0;
  1266.  
  1267.   while (pos > beg
  1268.      && ((code = SYNTAX (FETCH_CHAR (pos - 1))) == Scharquote
  1269.          || code == Sescape))
  1270.     pos--, quoted = !quoted;
  1271.   return quoted;
  1272. }
  1273.  
  1274. DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
  1275.   "Scan from character number FROM by COUNT lists.\n\
  1276. Returns the character number of the position thus found.\n\
  1277. \n\
  1278. If DEPTH is nonzero, paren depth begins counting from that value,\n\
  1279. only places where the depth in parentheses becomes zero\n\
  1280. are candidates for stopping; COUNT such places are counted.\n\
  1281. Thus, a positive value for DEPTH means go out levels.\n\
  1282. \n\
  1283. Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
  1284. \n\
  1285. If the beginning or end of (the accessible part of) the buffer is reached\n\
  1286. and the depth is wrong, an error is signaled.\n\
  1287. If the depth is right but the count is not used up, nil is returned.")
  1288.   (from, count, depth)
  1289.      Lisp_Object from, count, depth;
  1290. {
  1291.   CHECK_NUMBER (from, 0);
  1292.   CHECK_NUMBER (count, 1);
  1293.   CHECK_NUMBER (depth, 2);
  1294.  
  1295.   return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
  1296. }
  1297.  
  1298. DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
  1299.   "Scan from character number FROM by COUNT balanced expressions.\n\
  1300. If COUNT is negative, scan backwards.\n\
  1301. Returns the character number of the position thus found.\n\
  1302. \n\
  1303. Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
  1304. \n\
  1305. If the beginning or end of (the accessible part of) the buffer is reached\n\
  1306. in the middle of a parenthetical grouping, an error is signaled.\n\
  1307. If the beginning or end is reached between groupings\n\
  1308. but before count is used up, nil is returned.")
  1309.   (from, count)
  1310.      Lisp_Object from, count;
  1311. {
  1312.   CHECK_NUMBER (from, 0);
  1313.   CHECK_NUMBER (count, 1);
  1314.  
  1315.   return scan_lists (XINT (from), XINT (count), 0, 1);
  1316. }
  1317.  
  1318. DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
  1319.   0, 0, 0,
  1320.   "Move point backward over any number of chars with prefix syntax.\n\
  1321. This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
  1322.   ()
  1323. {
  1324.   int beg = BEGV;
  1325.   int pos = point;
  1326.  
  1327.   while (pos > beg && !char_quoted (pos - 1)
  1328.      && (SYNTAX (FETCH_CHAR (pos - 1)) == Squote
  1329.          || SYNTAX_PREFIX (FETCH_CHAR (pos - 1))))
  1330.     pos--;
  1331.  
  1332.   SET_PT (pos);
  1333.  
  1334.   return Qnil;
  1335. }
  1336.  
  1337. /* Parse forward from FROM to END,
  1338.    assuming that FROM has state OLDSTATE (nil means FROM is start of function),
  1339.    and return a description of the state of the parse at END.
  1340.    If STOPBEFORE is nonzero, stop at the start of an atom.
  1341.    If COMMENTSTOP is nonzero, stop at the start of a comment.  */
  1342.  
  1343. static void
  1344. scan_sexps_forward (stateptr, from, end, targetdepth,
  1345.             stopbefore, oldstate, commentstop)
  1346.      struct lisp_parse_state *stateptr;
  1347.      register int from;
  1348.      int end, targetdepth, stopbefore;
  1349.      Lisp_Object oldstate;
  1350.      int commentstop;
  1351. {
  1352.   struct lisp_parse_state state;
  1353.  
  1354.   register enum syntaxcode code;
  1355.   struct level { int last, prev; };
  1356.   struct level levelstart[100];
  1357.   register struct level *curlevel = levelstart;
  1358.   struct level *endlevel = levelstart + 100;
  1359.   char prev;
  1360.   register int depth;    /* Paren depth of current scanning location.
  1361.                level - levelstart equals this except
  1362.                when the depth becomes negative.  */
  1363.   int mindepth;        /* Lowest DEPTH value seen.  */
  1364.   int start_quoted = 0;        /* Nonzero means starting after a char quote */
  1365.   Lisp_Object tem;
  1366.  
  1367.   immediate_quit = 1;
  1368.   QUIT;
  1369.  
  1370.   if (NILP (oldstate))
  1371.     {
  1372.       depth = 0;
  1373.       state.instring = -1;
  1374.       state.incomment = 0;
  1375.       state.comstyle = 0;    /* comment style a by default */
  1376.     }
  1377.   else
  1378.     {
  1379.       tem = Fcar (oldstate);
  1380.       if (!NILP (tem))
  1381.     depth = XINT (tem);
  1382.       else
  1383.     depth = 0;
  1384.  
  1385.       oldstate = Fcdr (oldstate);
  1386.       oldstate = Fcdr (oldstate);
  1387.       oldstate = Fcdr (oldstate);
  1388.       tem = Fcar (oldstate);
  1389.       state.instring = !NILP (tem) ? XINT (tem) : -1;
  1390.  
  1391.       oldstate = Fcdr (oldstate);
  1392.       tem = Fcar (oldstate);
  1393.       state.incomment = !NILP (tem);
  1394.  
  1395.       oldstate = Fcdr (oldstate);
  1396.       tem = Fcar (oldstate);
  1397.       start_quoted = !NILP (tem);
  1398.  
  1399.       /* if the eight element of the list is nil, we are in comment
  1400.      style a. if it is non-nil, we are in comment style b */
  1401.       oldstate = Fcdr (oldstate);
  1402.       oldstate = Fcdr (oldstate);
  1403.       oldstate = Fcdr (oldstate);
  1404.       tem = Fcar (oldstate);
  1405.       state.comstyle = !NILP (tem);
  1406.     }
  1407.   state.quoted = 0;
  1408.   mindepth = depth;
  1409.  
  1410.   curlevel->prev = -1;
  1411.   curlevel->last = -1;
  1412.  
  1413.   /* Enter the loop at a place appropriate for initial state. */
  1414.  
  1415.   if (state.incomment) goto startincomment;
  1416.   if (state.instring >= 0)
  1417.     {
  1418.       if (start_quoted) goto startquotedinstring;
  1419.       goto startinstring;
  1420.     }
  1421.   if (start_quoted) goto startquoted;
  1422.  
  1423.   while (from < end)
  1424.     {
  1425.       code = SYNTAX (FETCH_CHAR (from));
  1426.       from++;
  1427.       if (code == Scomment)
  1428.     state.comstart = from-1;
  1429.       
  1430.       else if (from < end && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from - 1))
  1431.            && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from)))
  1432.     {
  1433.       /* Record the comment style we have entered so that only
  1434.          the comment-end sequence of the same style actually
  1435.          terminates the comment section.  */
  1436.       code = Scomment;
  1437.       state.comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from));
  1438.       state.comstart = from-1;
  1439.       from++;
  1440.     }
  1441.  
  1442.       if (SYNTAX_PREFIX (FETCH_CHAR (from - 1)))
  1443.     continue;
  1444. #ifdef SWITCH_ENUM_BUG
  1445.       switch ((int) code)
  1446. #else
  1447.       switch (code)
  1448. #endif
  1449.     {
  1450.     case Sescape:
  1451.     case Scharquote:
  1452.       if (stopbefore) goto stop;  /* this arg means stop at sexp start */
  1453.       curlevel->last = from - 1;
  1454.     startquoted:
  1455.       if (from == end) goto endquoted;
  1456.       from++;
  1457.       goto symstarted;
  1458.       /* treat following character as a word constituent */
  1459.     case Sword:
  1460.     case Ssymbol:
  1461.       if (stopbefore) goto stop;  /* this arg means stop at sexp start */
  1462.       curlevel->last = from - 1;
  1463.     symstarted:
  1464.       while (from < end)
  1465.         {
  1466. #ifdef SWITCH_ENUM_BUG
  1467.           switch ((int) SYNTAX (FETCH_CHAR (from)))
  1468. #else
  1469.           switch (SYNTAX (FETCH_CHAR (from)))
  1470. #endif
  1471.         {
  1472.         case Scharquote:
  1473.         case Sescape:
  1474.           from++;
  1475.           if (from == end) goto endquoted;
  1476.           break;
  1477.         case Sword:
  1478.         case Ssymbol:
  1479.         case Squote:
  1480.           break;
  1481.         default:
  1482.           goto symdone;
  1483.         }
  1484.           from++;
  1485.         }
  1486.     symdone:
  1487.       curlevel->prev = curlevel->last;
  1488.       break;
  1489.  
  1490.     case Scomment:
  1491.       state.incomment = 1;
  1492.     startincomment:
  1493.       if (commentstop)
  1494.         goto done;
  1495.       while (1)
  1496.         {
  1497.           if (from == end) goto done;
  1498.           prev = FETCH_CHAR (from);
  1499.           if (SYNTAX (prev) == Sendcomment
  1500.           && SYNTAX_COMMENT_STYLE (prev) == state.comstyle)
  1501.         /* Only terminate the comment section if the endcomment
  1502.            of the same style as the start sequence has been
  1503.            encountered.  */
  1504.         break;
  1505.           from++;
  1506.           if (from < end && SYNTAX_COMEND_FIRST (prev)
  1507.           && SYNTAX_COMEND_SECOND (FETCH_CHAR (from))
  1508.           && SYNTAX_COMMENT_STYLE (prev) == state.comstyle)
  1509.         /* Only terminate the comment section if the end-comment
  1510.            sequence of the same style as the start sequence has
  1511.            been encountered.  */
  1512.         { from++; break; }
  1513.         }
  1514.       state.incomment = 0;
  1515.       state.comstyle = 0;    /* reset the comment style */
  1516.       break;
  1517.  
  1518.     case Sopen:
  1519.       if (stopbefore) goto stop;  /* this arg means stop at sexp start */
  1520.       depth++;
  1521.       /* curlevel++->last ran into compiler bug on Apollo */
  1522.       curlevel->last = from - 1;
  1523.       if (++curlevel == endlevel)
  1524.         error ("Nesting too deep for parser");
  1525.       curlevel->prev = -1;
  1526.       curlevel->last = -1;
  1527.       if (!--targetdepth) goto done;
  1528.       break;
  1529.  
  1530.     case Sclose:
  1531.       depth--;
  1532.       if (depth < mindepth)
  1533.         mindepth = depth;
  1534.       if (curlevel != levelstart)
  1535.         curlevel--;
  1536.       curlevel->prev = curlevel->last;
  1537.       if (!++targetdepth) goto done;
  1538.       break;
  1539.  
  1540.     case Sstring:
  1541.       if (stopbefore) goto stop;  /* this arg means stop at sexp start */
  1542.       curlevel->last = from - 1;
  1543.       state.instring = FETCH_CHAR (from - 1);
  1544.     startinstring:
  1545.       while (1)
  1546.         {
  1547.           if (from >= end) goto done;
  1548.           if (FETCH_CHAR (from) == state.instring) break;
  1549. #ifdef SWITCH_ENUM_BUG
  1550.           switch ((int) SYNTAX (FETCH_CHAR (from)))
  1551. #else
  1552.           switch (SYNTAX (FETCH_CHAR (from)))
  1553. #endif
  1554.         {
  1555.         case Scharquote:
  1556.         case Sescape:
  1557.           from++;
  1558.         startquotedinstring:
  1559.           if (from >= end) goto endquoted;
  1560.         }
  1561.           from++;
  1562.         }
  1563.       state.instring = -1;
  1564.       curlevel->prev = curlevel->last;
  1565.       from++;
  1566.       break;
  1567.  
  1568.     case Smath:
  1569.       break;
  1570.     }
  1571.     }
  1572.   goto done;
  1573.  
  1574.  stop:   /* Here if stopping before start of sexp. */
  1575.   from--;    /* We have just fetched the char that starts it; */
  1576.   goto done; /* but return the position before it. */
  1577.  
  1578.  endquoted:
  1579.   state.quoted = 1;
  1580.  done:
  1581.   state.depth = depth;
  1582.   state.mindepth = mindepth;
  1583.   state.thislevelstart = curlevel->prev;
  1584.   state.prevlevelstart
  1585.     = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
  1586.   state.location = from;
  1587.   immediate_quit = 0;
  1588.  
  1589.   *stateptr = state;
  1590. }
  1591.  
  1592. /* This comment supplies the doc string for parse-partial-sexp,
  1593.    for make-docfile to see.  We cannot put this in the real DEFUN
  1594.    due to limits in the Unix cpp.
  1595.  
  1596. DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0,
  1597.   "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
  1598. Parsing stops at TO or when certain criteria are met;\n\
  1599.  point is set to where parsing stops.\n\
  1600. If fifth arg STATE is omitted or nil,\n\
  1601.  parsing assumes that FROM is the beginning of a function.\n\
  1602. Value is a list of eight elements describing final state of parsing:\n\
  1603.  1. depth in parens.\n\
  1604.  2. character address of start of innermost containing list; nil if none.\n\
  1605.  3. character address of start of last complete sexp terminated.\n\
  1606.  4. non-nil if inside a string.\n\
  1607.     (it is the character that will terminate the string.)\n\
  1608.  5. t if inside a comment.\n\
  1609.  6. t if following a quote character.\n\
  1610.  7. the minimum paren-depth encountered during this scan.\n\
  1611.  8. t if in a comment of style `b'.\n\
  1612. If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
  1613. in parentheses becomes equal to TARGETDEPTH.\n\
  1614. Fourth arg STOPBEFORE non-nil means stop when come to\n\
  1615.  any character that starts a sexp.\n\
  1616. Fifth arg STATE is a seven-list like what this function returns.\n\
  1617. It is used to initialize the state of the parse.  Its second and third
  1618. elements are ignored.
  1619. Sixth args COMMENTSTOP non-nil means stop at the start of a comment.")
  1620.   (from, to, targetdepth, stopbefore, state, commentstop)
  1621. */
  1622.  
  1623. DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
  1624.   0 /* See immediately above */)
  1625.   (from, to, targetdepth, stopbefore, oldstate, commentstop)
  1626.      Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
  1627. {
  1628.   struct lisp_parse_state state;
  1629.   int target;
  1630.  
  1631.   if (!NILP (targetdepth))
  1632.     {
  1633.       CHECK_NUMBER (targetdepth, 3);
  1634.       target = XINT (targetdepth);
  1635.     }
  1636.   else
  1637.     target = -100000;        /* We won't reach this depth */
  1638.  
  1639.   validate_region (&from, &to);
  1640.   scan_sexps_forward (&state, XINT (from), XINT (to),
  1641.               target, !NILP (stopbefore), oldstate,
  1642.               !NILP (commentstop));
  1643.  
  1644.   SET_PT (state.location);
  1645.   
  1646.   return Fcons (make_number (state.depth),
  1647.        Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
  1648.          Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
  1649.            Fcons (state.instring >= 0 ? make_number (state.instring) : Qnil,
  1650.          Fcons (state.incomment ? Qt : Qnil,
  1651.            Fcons (state.quoted ? Qt : Qnil,
  1652.               Fcons (make_number (state.mindepth),
  1653.                  Fcons (state.comstyle ? Qt : Qnil,
  1654.                     Qnil))))))));
  1655. }
  1656.  
  1657. _VOID_
  1658. init_syntax_once ()
  1659. {
  1660.   register int i;
  1661.   register struct Lisp_Vector *v;
  1662.  
  1663.   /* Set this now, so first buffer creation can refer to it. */
  1664.   /* Make it nil before calling copy-syntax-table
  1665.     so that copy-syntax-table will know not to try to copy from garbage */
  1666.   Vstandard_syntax_table = Qnil;
  1667.   Vstandard_syntax_table = Fcopy_syntax_table (Qnil);
  1668.  
  1669.   v = XVECTOR (Vstandard_syntax_table);
  1670.  
  1671.   for (i = 'a'; i <= 'z'; i++)
  1672.     XFASTINT (v->contents[i]) = (int) Sword;
  1673.   for (i = 'A'; i <= 'Z'; i++)
  1674.     XFASTINT (v->contents[i]) = (int) Sword;
  1675.   for (i = '0'; i <= '9'; i++)
  1676.     XFASTINT (v->contents[i]) = (int) Sword;
  1677.   XFASTINT (v->contents['$']) = (int) Sword;
  1678.   XFASTINT (v->contents['%']) = (int) Sword;
  1679.  
  1680.   XFASTINT (v->contents['(']) = (int) Sopen + (')' << 8);
  1681.   XFASTINT (v->contents[')']) = (int) Sclose + ('(' << 8);
  1682.   XFASTINT (v->contents['[']) = (int) Sopen + (']' << 8);
  1683.   XFASTINT (v->contents[']']) = (int) Sclose + ('[' << 8);
  1684.   XFASTINT (v->contents['{']) = (int) Sopen + ('}' << 8);
  1685.   XFASTINT (v->contents['}']) = (int) Sclose + ('{' << 8);
  1686.   XFASTINT (v->contents['"']) = (int) Sstring;
  1687.   XFASTINT (v->contents['\\']) = (int) Sescape;
  1688.  
  1689.   for (i = 0; i < 10; i++)
  1690.     XFASTINT (v->contents["_-+*/&|<>="[i]]) = (int) Ssymbol;
  1691.  
  1692.   for (i = 0; i < 12; i++)
  1693.     XFASTINT (v->contents[".,;:?!#@~^'`"[i]]) = (int) Spunct;
  1694. }
  1695.  
  1696. _VOID_
  1697. syms_of_syntax ()
  1698. {
  1699.   Qsyntax_table_p = intern ("syntax-table-p");
  1700.   staticpro (&Qsyntax_table_p);
  1701.  
  1702.   DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
  1703.     "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
  1704.  
  1705.   words_include_escapes = 0;
  1706.   DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
  1707.     "Non-nil means `forward-word', etc., should treat escape chars part of words.");
  1708.  
  1709.   defsubr (&Ssyntax_table_p);
  1710.   defsubr (&Ssyntax_table);
  1711.   defsubr (&Sstandard_syntax_table);
  1712.   defsubr (&Scopy_syntax_table);
  1713.   defsubr (&Sset_syntax_table);
  1714.   defsubr (&Schar_syntax);
  1715.   defsubr (&Smodify_syntax_entry);
  1716.   defsubr (&Sdescribe_syntax);
  1717.  
  1718.   defsubr (&Sforward_word);
  1719.  
  1720.   defsubr (&Sforward_comment);
  1721.   defsubr (&Sscan_lists);
  1722.   defsubr (&Sscan_sexps);
  1723.   defsubr (&Sbackward_prefix_chars);
  1724.   defsubr (&Sparse_partial_sexp);
  1725. }
  1726.